<?

class LoggerBrowscap
{
    protected static $operable;
    protected static $browscapIni;
    protected static $cache;

    public function __construct() 
    {
        if (!is_array(self::$cache))
            self::$cache = array();

        $browscapIniFile = dirname(__FILE__) . '/browscap.ini';
        $browscapIniUrl = 'http://browsers.garykeith.com/stream.asp?Full_PHP_BrowscapINI';

        if (!isset(self::$browscapIni))
        {
            self::$browscapIni = array();
            self::$operable = false;
            if (file_exists($browscapIniFile))
            {
                if (LoggerConfig::$updateExtDatabases && is_writable($browscapIniFile))
                {
                    $fileMTime = @filemtime($browscapIniFile);
                    if ($fileMTime !== false && $fileMTime < (time() - 5*24*3600))
                    {
                        $newContent = @file_get_contents($browscapIniUrl);
                        if ($newContent !== false)
                        {
                            $fp = @fopen($browscapIniFile, 'w');
                            if ($fp)
                            {
                                fwrite($fp, $newContent);
                                fclose($fp);
                            }
                        }
                    }
                }

                if (version_compare(PHP_VERSION, '5.3.0', '>='))
                    $parseResult = parse_ini_file($browscapIniFile, true, INI_SCANNER_RAW);
                else
                    $parseResult = parse_ini_file($browscapIniFile, true);

                if ($parseResult !== false)
                {
                    $parentSections = array();
                    foreach ($parseResult as $key => $browser)
                    {
                        if ($key == '*' || $key == 'GJK_Browscap_Version')
                            continue;
                        if (isset($browser['Parent']))
                            $parentSections[$browser['Parent']] = true;
                    }

                    $search = array('.', '?', '*', '+', '(', ')', '[', ']');
                    $replace = array('\.', '.', '.*', '\+', '\(', '\)', '\[', '\]');
                    foreach ($parseResult as $key => $browser)
                    {
                        if ($key == '*' || $key == 'GJK_Browscap_Version')
                            continue;
                        if (!isset($parentSections[$key]))
                            $browser['Pattern'] = '#^' . str_replace($search, $replace, $key) . '$#i';
                        self::$browscapIni[$key] = $browser;
                    }
                    self::$operable = true;
                }
            }
        }
    }

    public function operable()
    {
        return self::$operable;
    }

    public function getArray($agent)
    {
        if (!self::$operable)
            return false;

        if (isset(self::$cache[$agent]))
            return self::$cache[$agent];

        foreach (self::$browscapIni as $key => $array)
        {
            if (!isset($array['Pattern']))
                continue;
            $pattern = $array['Pattern'];

            if (preg_match($pattern, $agent))
            {
                while(isset($array['Parent']))
                {
                    $parent = $array['Parent'];
                    unset($array['Parent']);
                    if (isset(self::$browscapIni[$parent]))
                        $array = $array + self::$browscapIni[$parent];
                }
                self::$cache[$agent] = $array;
                return $array;
            }
        }
        self::$cache[$agent] = false;
        return false;
    }

    public function getRobot($agent)
    {
        $array = $this->getArray($agent);
        if (is_array($array) && isset($array['Crawler']) && $array['Crawler'] && isset($array['Browser']))
            return $array['Browser'];
        return false;
    }

    public function getBrowser($agent)
    {
        $array = $this->getArray($agent);
        if (is_array($array) && (!isset($array['Crawler']) || !$array['Crawler']) && isset($array['Browser']))
            return array(
                'name' => $array['Browser'],
                'ver' => $array['Version']
            );
        return false;
    }

    public function getPlatform($agent)
    {
        $array = $this->getArray($agent);
        if (is_array($array) && (!isset($array['Crawler']) || $array['Crawler'] != 'true') && isset($array['Platform']) && $array['Platform'] != 'unknown')
            return array(
                'family' => $array['Platform'],
                'name' => $array['Platform']
            );
        return false;
    }
}